#include "common/head.h"

int main() {
    MYSQL *conn_ptr;
    MYSQL_RES *res_ptr;
    MYSQL_ROW sqlRow;
    MYSQL_FIELD *fd;
    int res;   

    // conn_ptr = mysql_init(NULL);
    // if (!conn_ptr) {
    //     fprintf(stderr, "error in mysql_init\n");
    //     exit(EXIT_FAILURE);
    // }
    // conn_ptr = mysql_real_connect(conn_ptr, "47.99.207.120", "root", "0331", "server_monitor", 0, 0, 0);
    // if (!conn_ptr) {
    //     fprintf(stderr, "connection failed!\n");
    //     exit(EXIT_FAILURE);
    // }
    conn_ptr = connectDB("47.99.207.120", "root", "0331", "server_monitor", 0);
    if (!conn_ptr) {
        fprintf(stderr, "error in mysql_init\n");
        exit(EXIT_FAILURE);
    }
    printf("connect successed!\n");
    if (res = mysql_query(conn_ptr, "SELECT id, name, ip, updateTime FROM `slaveInfo`")) { // 查询语句
        fprintf(stderr, "SELECT error: %s\n", mysql_error(conn_ptr));
        exit(EXIT_FAILURE);
    } 
    if (!(res_ptr = mysql_store_result(conn_ptr))) {
        fprintf(stderr, "get result failed\n");
        exit(EXIT_FAILURE);
    }
    printf("%lu Rows\n", (unsigned long)mysql_num_rows(res_ptr));
    int fields = mysql_num_fields(res_ptr);
    while (sqlRow = mysql_fetch_row(res_ptr)) { // 取出结果集
        for(int i = 0; i < fields; i++) {
            printf("%s\t", sqlRow[i]);
        }
        printf("\n");
    }
    mysql_free_result(res_ptr);
    mysql_close(conn_ptr);

    return 0;
}